package cn.itcast.controller;

import cn.itcast.Service.CourseService;
import cn.itcast.common.PageBean;
import cn.itcast.common.R;
import cn.itcast.domain.dto.CourseDTO;
import cn.itcast.domain.dto.PageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zsw
 * @Date: 2024/1/18 21:46
 */
@RestController
@RequestMapping("/course")
@CrossOrigin
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/getCourseByPage")
    public R getCourseByPage(PageDTO pageDTO) {
        PageBean courseList = courseService.getCourseList(pageDTO);
        return R.success(courseList);
    }

    @PostMapping("/addCourse")
    public R addCourse(@RequestBody CourseDTO courseDTO) {
        int i = courseService.addCourse(courseDTO);
       if (i >= 1) {
           return R.success();
       }
        return R.error("添加失败");
    }

    @DeleteMapping("/deleteCourse/{id}")
    public R deleteCourse(@PathVariable("id") Integer courseId) {
        int i = courseService.deleteCourseById(courseId);
        if (i >= 1) {
            return R.success();
        }
        return R.error("删除失败");
    }

    @PostMapping("/updateCourse")
    public R updateCourse(@RequestBody CourseDTO courseDTO) {
        int i = courseService.updateCourse(courseDTO);
        if (i >= 1) {
            return R.success();
        }
        return R.error("更新失败");
    }
}
